<template>
  <div class="login">
    <div class="login-from">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
        label-position="left">
        <h3>登录中心</h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <i slot="prefix" class="el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码">
            <i slot="prefix" class="el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-button type="primary" :loading="loading" class="login-btn" @click.native.prevent="handleLogin">登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' },
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
        ],
      },
      redirect: undefined,
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss">
.login {
  position: relative;
  width: 100%;
  height: 100%;
  background: #f8f8f8;
  &-img {
    background-size: cover;
    width: 100%;
    height: 100%;
  }
  &-from {
    width: 240px;
    height: 240px;
    position: absolute;
    right: 10%;
    top: 35%;
    border-radius: 10px;
    text-align: center;
    z-index: 99;
    h3 {
      margin-bottom: 15px;
    }
    .el-form-item__error {
      padding-left: 20px;
    }
    .el-input {
      border-radius: 20px;
      overflow: hidden;
      display: block;
      .input-icon {
        padding-left: 10px;
        img {
          width: 16px;
          vertical-align: -4px;
        }
      }
      .el-input__inner {
        padding-left: 35px;
        background-color: rgba(0, 0, 0, 0.6);
        border-color: transparent !important;
        color: #fff;
        &:-webkit-autofill {
          -webkit-text-fill-color: #ffffff;
          caret-color: white;
          transition: background-color 0s linear 3600s;
        }
      }
    }
    .el-input__prefix {
      left: 15px;
      font-size: 18px;
    }
  }
  &-btn {
    width: 100%;
    margin: 0 auto;
    display: block;
    border-radius: 20px;
    font-size: 16px;
    border-color: transparent;
    padding: 10px 20px;
  }
  input::-webkit-input-placeholder {
    color: rgba(255, 255, 255, 0.5);
  }
}
</style>
